#pragma once
#include <iostream>
#include <pthread.h>

class Mutex
{
public:
    Mutex()
    {
        pthread_mutex_init(&_lock, nullptr);
    }
    bool Lock()
    {
        int n = pthread_mutex_lock(&_lock);
        if (n == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool UnLock()
    {
        int n = pthread_mutex_unlock(&_lock);
        if (n == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    ~Mutex()
    {
        pthread_mutex_destroy(&_lock);
    }

private:
    pthread_mutex_t _lock;
};

class Lock
{
public:
    Lock(Mutex *mutex)
        : _mutex(mutex)
    {
        if(_mutex->Lock())
        {
            std::cout<<"上锁成功"<<std::endl;
        }
        else
        {
            std::cout<<"上锁失败"<<std::endl;
        }
    }
    ~Lock()
    {
        if(_mutex->UnLock())
        {
            std::cout<<"解锁成功"<<std::endl;
        }
        else
        {
            std::cout<<"解锁失败"<<std::endl;
        }
    }

private:
    Mutex *_mutex;
};